/**
 * 获取当前一周时间数据
 * */
const weekText = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
export const getThisWeekDates = () => {
  const today = new Date()
  const dayOfWeek = today.getDay()
  const startDate = new Date(today)
  startDate.setDate(startDate.getDate() - dayOfWeek)
  const dates = []
  for (let i = 0; i < 7; i++) {
    const date = new Date(startDate)
    date.setDate(date.getDate() + i)
    dates.push({
      text: weekText[i],
      date: date.toISOString().slice(0, 10)
    })
  }
  return {
    today: today.toISOString().slice(0, 10),
    dates
  }
}

// 获取年份筛选数据组
export const getYearFilterData = () => {
  const data = [
    {
      id: 2010,
      label: '2019-2010',
      startTime: 2010,
      endTime: 2019
    },
    {
      id: 2000,
      label: '2009-2000',
      startTime: 2000,
      endTime: 2009
    },
    {
      id: 1990,
      label: '90年代',
      startTime: 1990,
      endTime: 1999
    },
    {
      id: 1980,
      label: '80年代',
      startTime: 1980,
      endTime: 1989
    },
    {
      id: 1970,
      label: '更早',
      startTime: 1970,
      endTime: 1979
    }
  ]
  const year = new Date().getFullYear()
  const arr = []
  for (let i = year; i >= 2020; i--) {
    arr.push({
      id: i,
      label: `${i}年`,
      startTime: i,
      endTime: i
    })
  }
  const all = {
    label: '全部',
    id: 'all'
  }
  return [all, ...arr, ...data]
}

/**
 * 筛选比当天少七天的数据
 * */
let days7 = 7 * 86400
export const filterPrevious7days = arr => {
  const now = parseInt(new Date().valueOf() / 1000)
  let latestArr = arr.filter(item => {
    let diff = now - item.playTime
    return diff <= days7
  })
  let oldArr = arr.filter(item => {
    let diff = now - item.playTime
    return diff > days7
  })
  return [latestArr, oldArr]
}

export const coverPosition = time => {
  return ('0' + time).slice(-2)
}

// 获取倒计时时间
export const diffRemainTimeMs = (startTime, endTime) => {
  const diffMs = new Date(endTime).valueOf() - new Date(startTime).valueOf()
  if (diffMs <= 0) {
    return undefined
  }
  const diffSec = Math.floor(diffMs / 1000)
  const d = Math.floor(diffSec / (24 * 60 * 60))
  const h = Math.floor((diffSec % (24 * 60 * 60)) / (60 * 60))
  const m = Math.floor(((diffSec % (24 * 60 * 60)) % (60 * 60)) / 60)
  const s = ((diffSec % (24 * 60 * 60)) % (60 * 60)) % 60
  return {
    d: d < 10 ? coverPosition(d) : d,
    h: coverPosition(h),
    m: coverPosition(m),
    s: coverPosition(s)
  }
}

/**
 * 时间格式化
 * */
export const formatDate = time => {
  if (!time) {
    return {}
  }
  const date = new Date(time)
  return {
    year: date.getFullYear(),
    month: coverPosition(date.getMonth() + 1),
    day: coverPosition(date.getDate()),
    hour: coverPosition(date.getHours()),
    minutes: coverPosition(date.getMinutes()),
    seconds: coverPosition(date.getSeconds())
  }
}

export const TODAY = () => {
  const { year, month, day } = formatDate(new Date())
  return '' + year + month + day
}

export const secondsToObj = seconds => {
  if (isNaN(seconds)) {
    return {}
  }
  const date = new Date(seconds * 1000)
  const obj = {
    year: date.getFullYear(),
    month: date.getMonth() + 1,
    day: date.getDate(),
    hour: date.getHours(),
    minute: date.getMinutes(),
    second: date.getSeconds()
  }
  obj.expire = `${obj.year}-${obj.month.toString().padStart(2, '0')}-${obj.day.toString().padStart(2, '0')}`
  return obj
}
